(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:QueuingCtrl
     * @description
     * # QueuingCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('QueuingCtrl', QueuingCtrl);

    QueuingCtrl.$inject = ['$scope', 'dataService', 'helper', 'dhfprinterService', '$timeout', '$interval', '$rootScope', '$state', 'moment'];

    function QueuingCtrl($scope, dataService, helper, dhfprinterService, $timeout, $interval, $rootScope, $state, moment) {
        $scope.listAction = function() {

            $scope.store_id = $rootScope.store_id

            //查询所有的桌子的人数，然后组成数组排列

            // var stopTime;
            // getshopData();
            // stopTime = $interval(getshopData, 3000);
            // $scope.$on("$destroy", function() {
            //     //清除配置,不然scroll会重复请求
            //     $interval.cancel(stopTime);
            // })
            $scope.getlist();
        }

        $scope.getlist = function() {
            $scope.quedata = [
                { name: "2人桌预约排队", num: 123, id: 1 }, { name: "疯狂抢购", num: 123, id: 3 }
            ];

            console.error("$scope.que", $scope.quedata);
        }
        $scope.addAction = function() {
            $scope.que = {
                "start_time": moment().format('YYYY-MM-DD 00:00:00'),
                "end_time": moment().format('YYYY-MM-DD 23:59:59'),
            };
        }

        $scope.infoAction = function() {
            $scope.reservation = [
                { open_id: "333333", no: 1, time: moment().format('YYYY-MM-DD 06:05:05') }, { open_id: "疯狂抢购", no: 2, time: moment().format('YYYY-MM-DD 10:20:30') }
            ];
        }

        function getshopData() {
            $scope.getdesk().then(function() {
                async.map($scope.desklist, function(data, callback) {
                    dataService.shopconfigGet({ query: { 'field': $scope.store_id + "_" + data.type + "_paidu" } }).then(function(reda) {
                        if (reda.shopconfigs[0]) {
                            var aa = reda.shopconfigs[0].value;
                            data.value = JSON.parse(aa);
                            data.shop_config_id = reda.shopconfigs[0].shop_config_id;
                        }
                        callback(null, null);
                    })
                }, function(e, r) {
                    // console.error("redareda",r);
                });
            });
        }

        $scope.getdesk = function() {

            $scope.key = "desk_" + $scope.store_id;
            return dataService.shopconfigGet({ query: { 'field': $scope.key + "_content" } }).then(function(result) {
                if (result.shopconfigs[0]) {
                    var aa = result.shopconfigs[0].value;
                    $scope.zhuozi = JSON.parse(aa);

                    $scope.Tpl = {};
                    //根据区分组
                    _.map($scope.zhuozi, function(dd, kk) {
                        if (!$scope.Tpl) {
                            if (dd.number) {
                                $scope.Tpl[dd.number].push(dd);
                            }
                        }
                        if ($scope.Tpl[dd.number]) {
                            $scope.Tpl[dd.number].push(dd);
                        } else {
                            if (dd.number) {
                                $scope.Tpl[dd.number] = [];
                                $scope.Tpl[dd.number].push(dd);
                            }
                        }
                    });
                    $scope.desklist = {};
                    _.map($scope.Tpl, function(da, ke) {
                        $scope.desklist[ke] = { len: da.length, type: ke };
                    });
                    console.error("$scope.$scope.desklist", $scope.desklist);
                    return $scope.desklist;
                }
            });
        }

        //排队处理
        $scope.delData = function(ke) {
            _.map($scope.desklist, function(da, key) {
                if (ke == key) {
                    da.value.shift();
                    dataService.shopConfigUpdate({ field: $scope.store_id + "_" + da.type + "_paidu", value: da.value, "shop_config_id": da.shop_config_id }).then(function(re1) {
                        console.error("re1", re1);
                    });
                }
            });
        }

        $scope.saveData = function() {
            console.error("$sce.que", $scope.que);
            helper.myAlert("success ");
            // {start_time: "2017-08-14 00:00:00", end_time: "2017-08-14 23:59:59", name: "玩玩去", num: 123}
        }
    }
})();